home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / src / out-of-phase-102-c / OutOfPhase 1.02 Source / OutOfPhase Folder / CmdDlgOneBinaryChoice.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-23  |  8.5 KB  |  286 lines  |  [TEXT/KAHL]

  1. /* CmdDlgOneBinaryChoice.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
  5. /*    Copyright (C) 1994  Thomas R. Lawrence                                 */
  6. /*                                                                           */
  7. /*    This program is free software; you can redistribute it and/or modify   */
  8. /*    it under the terms of the GNU General Public License as published by   */
  9. /*    the Free Software Foundation; either version 2 of the License, or      */
  10. /*    (at your option) any later version.                                    */
  11. /*                                                                           */
  12. /*    This program is distributed in the hope that it will be useful,        */
  13. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  14. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
  15. /*    GNU General Public License for more details.                           */
  16. /*                                                                           */
  17. /*    You should have received a copy of the GNU General Public License      */
  18. /*    along with this program; if not, write to the Free Software            */
  19. /*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
  20. /*                                                                           */
  21. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  22. /*                                                                           */
  23. /*****************************************************************************/
  24.  
  25. #include "MiscInfo.h"
  26. #include "Audit.h"
  27. #include "Debug.h"
  28. #include "Definitions.h"
  29.  
  30. #include "CmdDlgOneBinaryChoice.h"
  31. #include "Memory.h"
  32. #include "Screen.h"
  33. #include "EventLoop.h"
  34. #include "Menus.h"
  35. #include "SimpleButton.h"
  36. #include "WrapTextBox.h"
  37. #include "DataMunging.h"
  38. #include "Alert.h"
  39. #include "RadioButton.h"
  40.  
  41.  
  42. #define WINXSIZE (350)
  43.  
  44. #define PROMPTX (10)
  45. #define PROMPTY (5)
  46. #define PROMPTWIDTH (WINXSIZE - (2 * PROMPTX))
  47. #define PROMPTHEIGHT (50)
  48.  
  49. #define TRUEBUTTONX (PROMPTX)
  50. #define TRUEBUTTONY (PROMPTY + PROMPTHEIGHT + 5)
  51. #define TRUEBUTTONWIDTH (PROMPTWIDTH)
  52. #define TRUEBUTTONHEIGHT (19)
  53.  
  54. #define FALSEBUTTONX (TRUEBUTTONX)
  55. #define FALSEBUTTONY (TRUEBUTTONY + TRUEBUTTONHEIGHT + 2)
  56. #define FALSEBUTTONWIDTH (TRUEBUTTONWIDTH)
  57. #define FALSEBUTTONHEIGHT (TRUEBUTTONHEIGHT)
  58.  
  59. #define CANCELBUTTONWIDTH (80)
  60. #define CANCELBUTTONHEIGHT (21)
  61. #define CANCELBUTTONX ((1 * WINXSIZE) / 4 - (CANCELBUTTONWIDTH / 2))
  62. #define CANCELBUTTONY (FALSEBUTTONY + FALSEBUTTONHEIGHT + 10)
  63.  
  64. #define OKBUTTONWIDTH (CANCELBUTTONWIDTH)
  65. #define OKBUTTONHEIGHT (CANCELBUTTONHEIGHT)
  66. #define OKBUTTONX ((3 * WINXSIZE) / 4 - (OKBUTTONWIDTH / 2))
  67. #define OKBUTTONY (CANCELBUTTONY)
  68.  
  69. #define WINYSIZE (CANCELBUTTONY + CANCELBUTTONHEIGHT + 20)
  70.  
  71.  
  72. typedef struct
  73.     {
  74.         WinType*                    ScreenID;
  75.         char*                            PromptText;
  76.         RadioButtonRec*        TrueButton;
  77.         RadioButtonRec*        FalseButton;
  78.         SimpleButtonRec*    OKButton;
  79.         SimpleButtonRec*    CancelButton;
  80.     } WindowRec;
  81.  
  82.  
  83. static void            RedrawWindow(WindowRec* Window)
  84.     {
  85.         CheckPtrExistence(Window);
  86.         RedrawRadioButton(Window->TrueButton);
  87.         RedrawRadioButton(Window->FalseButton);
  88.         RedrawSimpleButton(Window->OKButton);
  89.         RedrawSimpleButton(Window->CancelButton);
  90.         SetClipRect(Window->ScreenID,0,0,WINXSIZE,WINYSIZE);
  91.         DrawWrappedTextBox(Window->ScreenID,Window->PromptText,GetScreenFont(),9,
  92.             PROMPTX,PROMPTY,PROMPTWIDTH,PROMPTHEIGHT);
  93.     }
  94.  
  95.  
  96. /* dialog box that gives the user a choice of two radio buttons.  it returns True */
  97. /* if the user accepted the change and there was a change. */
  98. MyBoolean                CommandDialogOneBinaryChoice(char* Prompt, char* TrueButtonName,
  99.                                     char* FalseButtonName, MyBoolean* FlagInOut)
  100.     {
  101.         WindowRec*        Window;
  102.         MyBoolean            LoopFlag;
  103.         MyBoolean            DoItFlag EXECUTE(= -31342);
  104.         MyBoolean            ReturnValue;
  105.  
  106.         Window = (WindowRec*)AllocPtrCanFail(sizeof(WindowRec),
  107.             "CommandDialogOneBinaryChoice:  WindowRec");
  108.         if (Window == NIL)
  109.             {
  110.              FailurePoint1:
  111.                 AlertHalt("There is not enough memory available to edit the "
  112.                     "command parameters.",NIL);
  113.                 return False;
  114.             }
  115.         Window->PromptText = Prompt;
  116.  
  117.         Window->ScreenID = MakeNewWindow(eModelessDialogWindow,eWindowNotClosable,
  118.             eWindowNotZoomable,eWindowNotResizable,DialogLeftEdge(WINXSIZE),
  119.             DialogTopEdge(WINYSIZE),WINXSIZE,WINYSIZE,(void (*)(void*))&RedrawWindow,Window);
  120.         if (Window->ScreenID == NIL)
  121.             {
  122.              FailurePoint2:
  123.                 ReleasePtr((char*)Window);
  124.                 goto FailurePoint1;
  125.             }
  126.         SetWindowName(Window->ScreenID,"Edit Command");
  127.  
  128.         Window->OKButton = NewSimpleButton(Window->ScreenID,"OK",OKBUTTONX,OKBUTTONY,
  129.             OKBUTTONWIDTH,OKBUTTONHEIGHT);
  130.         if (Window->OKButton == NIL)
  131.             {
  132.              FailurePoint3:
  133.                 KillWindow(Window->ScreenID);
  134.                 goto FailurePoint2;
  135.             }
  136.         SetDefaultButtonState(Window->OKButton,True);
  137.  
  138.         Window->CancelButton = NewSimpleButton(Window->ScreenID,"Cancel",CANCELBUTTONX,
  139.             CANCELBUTTONY,CANCELBUTTONWIDTH,CANCELBUTTONHEIGHT);
  140.         if (Window->CancelButton == NIL)
  141.             {
  142.              FailurePoint4:
  143.                 DisposeSimpleButton(Window->OKButton);
  144.                 goto FailurePoint3;
  145.             }
  146.  
  147.         Window->TrueButton = NewRadioButton(Window->ScreenID,TrueButtonName,
  148.             TRUEBUTTONX,TRUEBUTTONY,TRUEBUTTONWIDTH,TRUEBUTTONHEIGHT);
  149.         if (Window->TrueButton == NIL)
  150.             {
  151.              FailurePoint5:
  152.                 DisposeSimpleButton(Window->CancelButton);
  153.                 goto FailurePoint4;
  154.             }
  155.  
  156.         Window->FalseButton = NewRadioButton(Window->ScreenID,FalseButtonName,
  157.             FALSEBUTTONX,FALSEBUTTONY,FALSEBUTTONWIDTH,FALSEBUTTONHEIGHT);
  158.         if (Window->FalseButton == NIL)
  159.             {
  160.              FailurePoint6:
  161.                 DisposeRadioButton(Window->TrueButton);
  162.                 goto FailurePoint5;
  163.             }
  164.  
  165.         if (*FlagInOut)
  166.             {
  167.                 SetRadioButtonState(Window->TrueButton,True);
  168.             }
  169.          else
  170.             {
  171.                 SetRadioButtonState(Window->FalseButton,True);
  172.             }
  173.  
  174.  
  175.         LoopFlag = True;
  176.         while (LoopFlag)
  177.             {
  178.                 OrdType                            X;
  179.                 OrdType                            Y;
  180.                 ModifierFlags                Modifiers;
  181.                 MenuItemType*                MenuItem;
  182.                 char                                KeyPress;
  183.  
  184.                 switch (GetAnEvent(&X,&Y,&Modifiers,NIL,&MenuItem,&KeyPress))
  185.                     {
  186.                         default:
  187.                             break;
  188.                         case eCheckCursor:
  189.                             SetArrowCursor();
  190.                             break;
  191.                         case eNoEvent:
  192.                             break;
  193.                         case eMenuStarting:
  194.                             break;
  195.                         case eMenuCommand:
  196.                             EXECUTE(PRERR(AllowResume,
  197.                                 "CommandDialogOneBinaryChoice: Undefined menu option chosen"));
  198.                             break;
  199.                         case eKeyPressed:
  200.                             if (KeyPress == 13)
  201.                                 {
  202.                                     FlashButton(Window->OKButton);
  203.                                     DoItFlag = True;
  204.                                     LoopFlag = False;
  205.                                 }
  206.                             else if (KeyPress == eCancelKey)
  207.                                 {
  208.                                     FlashButton(Window->CancelButton);
  209.                                     DoItFlag = False;
  210.                                     LoopFlag = False;
  211.                                 }
  212.                             break;
  213.                         case eMouseDown:
  214.                             if (SimpleButtonHitTest(Window->OKButton,X,Y))
  215.                                 {
  216.                                     if (SimpleButtonMouseDown(Window->OKButton,X,Y,NIL,NIL))
  217.                                         {
  218.                                             DoItFlag = True;
  219.                                             LoopFlag = False;
  220.                                         }
  221.                                 }
  222.                             else if (SimpleButtonHitTest(Window->CancelButton,X,Y))
  223.                                 {
  224.                                     if (SimpleButtonMouseDown(Window->CancelButton,X,Y,NIL,NIL))
  225.                                         {
  226.                                             DoItFlag = False;
  227.                                             LoopFlag = False;
  228.                                         }
  229.                                 }
  230.                             else if (RadioButtonHitTest(Window->TrueButton,X,Y))
  231.                                 {
  232.                                     if (RadioButtonMouseDown(Window->TrueButton,X,Y))
  233.                                         {
  234.                                             SetRadioButtonState(Window->FalseButton,False);
  235.                                         }
  236.                                 }
  237.                             else if (RadioButtonHitTest(Window->FalseButton,X,Y))
  238.                                 {
  239.                                     if (RadioButtonMouseDown(Window->FalseButton,X,Y))
  240.                                         {
  241.                                             SetRadioButtonState(Window->TrueButton,False);
  242.                                         }
  243.                                 }
  244.                             break;
  245.                     }
  246.             }
  247.         ERROR((DoItFlag != True) && (DoItFlag != False),PRERR(ForceAbort,
  248.             "CommandDialogOneBinaryChoice:  DoItFlag is neither true nor false"));
  249.  
  250.         ReturnValue = False;
  251.  
  252.         if (DoItFlag)
  253.             {
  254.                 MyBoolean                    PossibleValue;
  255.  
  256.                 if (GetRadioButtonState(Window->TrueButton))
  257.                     {
  258.                         PossibleValue = True;
  259.                     }
  260.                 else if (GetRadioButtonState(Window->FalseButton))
  261.                     {
  262.                         PossibleValue = False;
  263.                     }
  264.                 else
  265.                     {
  266.                         EXECUTE(PRERR(ForceAbort,
  267.                             "CommandDialogOneBinaryChoice:  button inconsistency"));
  268.                     }
  269.  
  270.                 if (PossibleValue != *FlagInOut)
  271.                     {
  272.                         ReturnValue = True;
  273.                         *FlagInOut = PossibleValue;
  274.                     }
  275.             }
  276.  
  277.         DisposeSimpleButton(Window->OKButton);
  278.         DisposeSimpleButton(Window->CancelButton);
  279.         DisposeRadioButton(Window->TrueButton);
  280.         DisposeRadioButton(Window->FalseButton);
  281.         KillWindow(Window->ScreenID);
  282.         ReleasePtr((char*)Window);
  283.  
  284.         return ReturnValue;
  285.     }
  286.